﻿namespace HuaWei.CommClass
{
    using DevExpress.XtraGrid;
    using DevExpress.XtraTreeList;
    using System;
    using System.Data;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Xml;

    public static class DHelp
    {
        public static void AddColumnSelect(DataTable table)
        {
            DataColumn column = new DataColumn("IsSelect");
            column.DataType = System.Type.GetType("System.Boolean");
            column.DefaultValue = false;
            table.Columns.Add(column);
        }

        public static DataRow AddRow(DataTable dt)
        {
            DataRow row = dt.NewRow();
            dt.Rows.Add(row);
            return row;
        }

        public static int BSFind(BindingSource bs, string propertyName, object key)
        {
            int num = -1;
            if ((bs != null) && (bs.List.Count > 0))
            {
                num = bs.Find(propertyName, key);
                bs.Position = num;
            }
            return num;
        }

        public static DataSet ConvertXMLToDataSet(string xmlData)
        {
            StringReader input = null;
            XmlTextReader reader = null;
            DataSet set2;
            try
            {
                DataSet set = new DataSet();
                input = new StringReader(xmlData);
                reader = new XmlTextReader(input);
                set.ReadXml(reader);
                set2 = set;
            }
            catch (Exception exception)
            {
                string message = exception.Message;
                set2 = null;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return set2;
        }

        public static void CopyData(BindingSource source, BindingSource target)
        {
            DataRow row = (source.Current as DataRowView).Row;
            DataRow row2 = ((DataRowView)target.AddNew()).Row;
            for (int i = 0; i < row.ItemArray.Length; i++)
            {
                row2[i] = row[i];
            }
            target.EndEdit();
        }

        public static void CopyDt(DataTable sDt, DataTable tDt)
        {
            foreach (DataRow row in sDt.Rows)
            {
                tDt.Rows.Add(row.ItemArray);
            }
        }

        public static void CopyDt(DataTable sDt, DataTable tDt, string[] fieldNames, object[] values)
        {
            foreach (DataRow row in sDt.Rows)
            {
                DataRow row2 = tDt.Rows.Add(row.ItemArray);
                string name = "";
                for (int i = 0; i < fieldNames.Length; i++)
                {
                    name = fieldNames[i];
                    if (tDt.Columns.Contains(name))
                    {
                        row2[name] = values[i];
                    }
                }
            }
        }

        public static DataRow CopyItemArray(DataTable tDt, DataRow row, [Optional, DefaultParameterValue(false)] bool delSourceRow)
        {
            DataRow row2;
            try
            {
                if (delSourceRow)
                {
                    DataRow[] rowArray = tDt.Select("id='" + row["id"].ToString() + "'");
                    if (rowArray.Length > 0)
                    {
                        tDt.Rows.Remove(rowArray[0]);
                    }
                }
                row2 = tDt.Rows.Add(row.ItemArray);
            }
            catch (Exception exception)
            {
                throw new Exception(tDt.TableName + ":" + exception.Message);
            }
            return row2;
        }

        public static DataRow CopyItemArrayOverlap(DataTable tDt, DataRow row, [Optional, DefaultParameterValue(false)] bool overlap)
        {
            DataRow row2;
            try
            {
                if (!overlap)
                {
                    DataRow[] rowArray = tDt.Select("id='" + row["id"].ToString() + "'");
                    if (rowArray.Length == 0)
                    {
                        return tDt.Rows.Add(row.ItemArray);
                    }
                    return rowArray[0];
                }
                row2 = tDt.Rows.Add(row.ItemArray);
            }
            catch (Exception exception)
            {
                throw new Exception(tDt.TableName + ":" + exception.Message);
            }
            return row2;
        }

        public static string CopyRow(DataRow sRow, DataRow tRow, [Optional, DefaultParameterValue("")] string pid, [Optional, DefaultParameterValue("")] string notFieldNames)
        {
            string str = Guid.NewGuid().ToString();
            string str2 = "";
            for (int i = 0; i < sRow.ItemArray.Length; i++)
            {
                str2 = sRow.Table.Columns[i].ColumnName.ToLower();
                if (!notFieldNames.Contains("#" + str2 + "#"))
                {
                    if (str2.Equals("id"))
                    {
                        if (tRow["id"].Equals(""))
                        {
                            tRow[i] = str;
                        }
                    }
                    else if (str2.Equals("pid"))
                    {
                        tRow[i] = pid;
                    }
                    else if (!str2.Equals("hvs"))
                    {
                        tRow[i] = sRow[i];
                    }
                }
            }
            tRow.EndEdit();
            return str;
        }

        public static string CopyRow(object[] itemArray, DataRow tRow, [Optional, DefaultParameterValue("")] string pid, [Optional, DefaultParameterValue("")] string notFieldNames)
        {
            string str = Guid.NewGuid().ToString();
            string str2 = "";
            for (int i = 0; i < itemArray.Length; i++)
            {
                str2 = tRow.Table.Columns[i].ColumnName.ToLower();
                if (!notFieldNames.Contains("#" + str2 + "#"))
                {
                    if (str2.Equals("id"))
                    {
                        if (tRow["id"].Equals(""))
                        {
                            tRow[i] = str;
                        }
                    }
                    else if (str2.Equals("pid"))
                    {
                        tRow[i] = pid;
                    }
                    else if (!str2.Equals("hvs"))
                    {
                        tRow[i] = itemArray[i];
                    }
                }
            }
            tRow.EndEdit();
            return str;
        }

        public static DataRow DataAddMxRow(BindingSource bsMx, BindingSource bsM)
        {
            string objId = GetObjId(bsM);
            DataView list = bsMx.List as DataView;
            DataRow row = list.AddNew().Row;
            row["id"] = Guid.NewGuid();
            row["pid"] = objId;
            bsMx.EndEdit();
            bsMx.MoveLast();
            return row;
        }

        public static DataRow DataAddNew(BindingSource source, [Optional, DefaultParameterValue("")] string fieldName, [Optional, DefaultParameterValue("")] string fieldValue)
        {
            DataView list = source.List as DataView;
            DataRow row = list.AddNew().Row;
            source.EndEdit();
            source.MoveLast();
            return row;
        }

        public static DataRow DataAddNewChild(BindingSource source, bool isBrother, [Optional, DefaultParameterValue("")] string bdid)
        {
            DataRow row = null;
            if (source.Current != null)
            {
                string str;
                row = (source.Current as DataRowView).Row;
                if (isBrother)
                {
                    str = row["pid"].ToString();
                }
                else
                {
                    str = row["id"].ToString();
                }
                DataView list = source.List as DataView;
                DataRow row2 = list.AddNew().Row;
                row2["id"] = Guid.NewGuid();
                row2["pid"] = str;
                if (!bdid.Equals(""))
                {
                    row2["bdid"] = bdid;
                }
                source.MoveLast();
                source.EndEdit();
            }
            return row;
        }

        public static void DBEdited(BindingSource bs)
        {
            if (bs.Count > bs.Position)
            {
                bs.Position++;
            }
            else
            {
                bs.Position--;
            }
        }

        public static void DelColumnSelect(DataTable table)
        {
            DataColumn column = table.Columns["IsSelect"];
            table.Columns.Remove(column);
        }

        public static void DelCurRow(BindingSource source)
        {
            source.RemoveCurrent();
            source.EndEdit();
        }

        public static void DelCurRow(Control Sender)
        {
            BindingManagerBase bMB = GetBMB(Sender);
            int position = bMB.Position;
            bMB.RemoveAt(position);
        }

        public static void DelDetailData(BindingSource source, string pid)
        {
            DataView list = source.List as DataView;
            foreach (DataRowView view2 in list)
            {
                if (view2.Row["pid"].ToString() == pid)
                {
                    source.Remove(view2);
                }
            }
        }

        public static BindingManagerBase GetBMB(Control Sender)
        {
            if (Sender is GridControl)
            {
                GridControl control = (GridControl)Sender;
                return control.BindingContext[control.DataSource, control.DataMember];
            }
            if (Sender is TreeList)
            {
                TreeList list = (TreeList)Sender;
                return list.BindingContext[list.DataSource, list.DataMember];
            }
            return null;
        }

        public static BindingManagerBase GetBMB(Form Frm, DataSet ds, string TName)
        {
            return (CurrencyManager)Frm.BindingContext[ds, TName];
        }

        public static object GetColumnValue(DataRowCollection rows, string field, [Optional, DefaultParameterValue(0)] int index)
        {
            if (rows.Count > 0)
            {
                return rows[index];
            }
            return null;
        }

        public static DataRow GetCurRow(Control Sender)
        {
            BindingManagerBase bMB = GetBMB(Sender);
            if (bMB.Position == -1)
            {
                return null;
            }
            return ((DataRowView)bMB.Current).Row;
        }

        public static DataRow GetDataRow(BindingSource source)
        {
            DataRow row = null;
            if (source.Current != null)
            {
                row = (source.Current as DataRowView).Row;
            }
            return row;
        }

        public static DataView GetDataView(BindingSource source)
        {
            return (source.List as DataView);
        }

        public static string GetDtFieldNames(DataTable dt)
        {
            string str = "";
            string columnName = "";
            foreach (DataColumn column in dt.Columns)
            {
                columnName = column.ColumnName;
                if (!columnName.Equals("HVS"))
                {
                    if (!str.Equals(""))
                    {
                        str = str + ",";
                    }
                    str = str + columnName;
                }
            }
            return str;
        }

        public static string GetFieldValue(DataTable dt, [Optional, DefaultParameterValue("id")] string fieldName, [Optional, DefaultParameterValue(0)] int position)
        {
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    return dt.Rows[position][fieldName].ToString();
                }
                return "";
            }
            return "";
        }

        public static string GetHVS(DataRow row)
        {
            return ("0x" + BitConverter.ToString((byte[])row["HVS"]).Replace("-", ""));
        }

        public static string GetHVSs(DataTable dt)
        {
            string str = "";
            foreach (DataRow row in dt.Rows)
            {
                if (str != "")
                {
                    str = str + ",";
                }
                str = str + "'0x" + BitConverter.ToString((byte[])row["HVS"]).Replace("-", "") + "'";
            }
            return str;
        }

        public static string GetIds(DataRow[] rows, [Optional, DefaultParameterValue("id")] string keyName)
        {
            string str = "";
            foreach (DataRow row in rows)
            {
                if (str != "")
                {
                    str = str + ",";
                }
                object obj2 = str;
                str = string.Concat(new object[] { obj2, "'", row[keyName], "'" });
            }
            return str;
        }

        public static string GetIds(DataTable dt, [Optional, DefaultParameterValue("id")] string keyName, [Optional, DefaultParameterValue("")] string condition)
        {
            DataTable table = dt.Copy();
            string str = "";
            if (table != null)
            {
                if (!condition.Equals(""))
                {
                    table.DefaultView.RowFilter = condition;
                }
                foreach (DataRowView view in table.DefaultView)
                {
                    DataRow row = view.Row;
                    if (str != "")
                    {
                        str = str + ",";
                    }
                    str = str + "'" + Convert.ToString(row[keyName]) + "'";
                }
            }
            if (str.Equals(""))
            {
                str = "''";
            }
            return str;
        }

        public static string GetIdsDistinct(DataTable dt, [Optional, DefaultParameterValue("id")] string keyName, [Optional, DefaultParameterValue("")] string condition)
        {
            DataTable table = dt.Copy();
            string str = "";
            if (table != null)
            {
                if (!condition.Equals(""))
                {
                    table.DefaultView.RowFilter = condition;
                }

                DataTable dtDistinct = table.DefaultView.ToTable(true, keyName);

                foreach (DataRow row in dtDistinct.Rows)
                {
                    if (str != "")
                    {
                        str = str + ",";
                    }
                    str = str + "'" + Convert.ToString(row[keyName]) + "'";
                }
            }
            if (str.Equals(""))
            {
                str = "''";
            }
            return str;
        }

        public static string GetObjId(BindingSource source)
        {
            if (source.Current != null)
            {
                return (source.Current as DataRowView).Row["id"].ToString();
            }
            return "id is null";
        }

        public static string GetObjValue(BindingSource source, string fieldName, [Optional, DefaultParameterValue(false)] bool isDecimal)
        {
            string str = "";
            if (source.Current != null)
            {
                object current = source.Current;
                if (((current is DataRowView) && (current as DataRowView).Row.Table.Columns.Contains(fieldName)) && !((current as DataRowView).Row[fieldName] is DBNull))
                {
                    str = (current as DataRowView).Row[fieldName].ToString();
                }
            }
            if (str.Equals("") && isDecimal)
            {
                str = "0";
            }
            return str;
        }

        public static int GetRowPos(Control Sender)
        {
            return GetBMB(Sender).Position;
        }

        public static object GetRowValue(DataRow[] rows, string field, [Optional, DefaultParameterValue(0)] int position)
        {
            if (rows[position] != null)
            {
                return rows[position][field];
            }
            return "";
        }

        public static string GetTreeSql(string tableName, string value, int level)
        {
            int num;
            int num2;
            string str = "";
            for (num = level; num > 0; num--)
            {
                if (!str.Equals(""))
                {
                    str = str + " union all ";
                }
                if (num > 1)
                {
                    str = str + "select * from " + tableName + " where id in (";
                    num2 = 0;
                    while (num2 < (num - 1))
                    {
                        if (num2 == (num - 2))
                        {
                            str = str + "select pid from " + tableName + " where id='#id#'";
                        }
                        else
                        {
                            str = str + "select pid from " + tableName + " where id in (";
                        }
                        num2++;
                    }
                    num2 = 0;
                    while (num2 < (num - 1))
                    {
                        str = str + ")";
                        num2++;
                    }
                }
                else
                {
                    str = str + "select * from " + tableName + " where id='#id#'";
                }
            }
            for (num = level; num > 0; num--)
            {
                if (!str.Equals(""))
                {
                    str = str + " union all ";
                }
                if (num > 1)
                {
                    str = str + "select * from " + tableName + " where pid in (";
                    num2 = 0;
                    while (num2 < (num - 1))
                    {
                        if (num2 == (num - 2))
                        {
                            str = str + "select id from " + tableName + " where pid='#id#'";
                        }
                        else
                        {
                            str = str + "select id from " + tableName + " where pid in (";
                        }
                        num2++;
                    }
                    for (num2 = 0; num2 < (num - 1); num2++)
                    {
                        str = str + ")";
                    }
                }
                else
                {
                    str = str + "select * from " + tableName + " where pid='#id#'";
                }
            }
            return str.Replace("#id#", value);
        }

        public static string GetTreeSql1(string tableName, string value, int level)
        {
            string str = "";
            for (int i = level; i > 0; i--)
            {
                if (!str.Equals(""))
                {
                    str = str + " union all ";
                }
                if (i > 1)
                {
                    str = str + "select * from " + tableName + " where id in (";
                    int num2 = 0;
                    while (num2 < (i - 1))
                    {
                        if (num2 == (i - 2))
                        {
                            str = str + "select pid from " + tableName + " where id='#id#'";
                        }
                        else
                        {
                            str = str + "select pid from " + tableName + " where id in (";
                        }
                        num2++;
                    }
                    for (num2 = 0; num2 < (i - 1); num2++)
                    {
                        str = str + ")";
                    }
                }
                else
                {
                    str = str + "select * from " + tableName + " where id='#id#'";
                }
            }
            return str.Replace("#id#", value);
        }

        public static decimal GetValue(DataRow row, string name)
        {
            decimal num = 0M;
            if (!(row[name] is DBNull))
            {
                num = Convert.ToDecimal(row[name]);
            }
            return num;
        }

        public static void SetFieldsDefault(object source)
        {
            DataRow row = source as DataRow;
            row["id"] = Guid.NewGuid();
        }

        public static DataRow smethod(BindingSource bs, string propertyName, object key)
        {
            int num = -1;
            if ((bs != null) && (bs.List.Count > 0))
            {
                num = bs.Find(propertyName, key);
                bs.Position = num;
            }
            if (bs.Current != null)
            {
                return (bs.Current as DataRowView).Row;
            }
            return null;
        }

        public static DataSet ToDataSetByXml(string xmlStr)
        {
            if (xmlStr.Length > 0)
            {
                StringReader input = null;
                XmlTextReader reader = null;
                try
                {
                    DataSet set = new DataSet();
                    input = new StringReader(xmlStr);
                    reader = new XmlTextReader(input);
                    set.ReadXml(reader, XmlReadMode.ReadSchema);
                    return set;
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                        input.Close();
                    }
                }
            }
            return null;
        }

        public static decimal ToDecimal(object obj)
        {
            decimal num = 0M;
            if (!(obj is DBNull) && !obj.Equals(""))
            {
                num = Convert.ToDecimal(obj);
            }
            return num;
        }
    }
}

